#include "AHT20.h"

static struct bflb_device_s *i2c;
static struct bflb_i2c_msg_s msgs[2];
/**************************Local Function**************************/
void IIC0_Init(void)
{
	struct bflb_device_s* gpio;
	
	gpio = bflb_device_get_by_name("gpio");
	/* I2C0_SCL */
	bflb_gpio_init(gpio, GPIO_PIN_0, GPIO_FUNC_I2C0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
	/* I2C0_SDA */
	bflb_gpio_init(gpio, GPIO_PIN_1, GPIO_FUNC_I2C0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
    
	/* IIC初始化，速度为400K */
	i2c = bflb_device_get_by_name("i2c0");
	bflb_i2c_init(i2c, 400000);
	
    //printf("IIC0 Init over\r\n");
}
void AHT20_SenCmd(uint8_t _Cmd, uint8_t *Sendbuf, uint16_t _length)
{
    msgs[0].addr = AHT20_ADDR;
    msgs[0].flags = I2C_M_NOSTOP;
    msgs[0].buffer = &_Cmd;
    msgs[0].length = 1;

	if (_length != 0)
	{
		/* 有数据的发送 */
     	msgs[1].addr = AHT20_ADDR;
     	msgs[1].flags = 0;
     	msgs[1].buffer = Sendbuf;
     	msgs[1].length = _length;
     	bflb_i2c_transfer(i2c, msgs, 2);
	}
	else
	{
		/* 只发一个字节的数据 */
		msgs[0].flags = 0;
		bflb_i2c_transfer(i2c, msgs, 1);
	}
}
void AHT20_RecData(uint8_t *Recbuf, uint16_t _length)
{
    msgs[0].addr = AHT20_ADDR;
    msgs[0].flags = I2C_M_READ;
    msgs[0].buffer = Recbuf;
    msgs[0].length = _length;
    bflb_i2c_transfer(i2c, msgs, 1);
}

void AHT20_ChipInit(void)
{
	uint8_t SendDataBuff[2] = {0x08, 0x00};
	 
	AHT20_SenCmd(AHT20_CMD_INIT, SendDataBuff, 2);
	
    //printf("Chip Init CMD send over\r\n");
}

void AHT20_ChipStartWork(void)
{
	uint8_t SendDataBuff[2] = {0x33, 0x00};
	 
	AHT20_SenCmd(AHT20_CMD_START, SendDataBuff, 2);
	
    //printf("Chip Start CMD send over\r\n");
}

void AHT20_ChipSoftRes(void)
{
	AHT20_SenCmd(AHT20_CMD_SOFT_RES, 0, 0);

	//printf("Chip Soft Res CMD send over\r\n");
}

/**************************Global Function**************************/

AHT20Sta AHT20_ReadSta(void)
{
	uint8_t ReadStaVal = 0;
	AHT20_RecData(&ReadStaVal, 1);
	if((ReadStaVal & 0x68) == 0x08) 
	{
		//printf("Chip Sta is OK\r\n");
		return AHT20_STA_READY;
	}
	else
	{
	    //printf("Chip State is Err !!!Receive State Value is %d\r\n", ReadStaVal);
		return AHT20_STA_ERR;
	}

}
AHT20InitRes AHT20_Init()
{
	uint8_t SoftResCount = 0;
	
	IIC0_Init();
	
	/* 延时40ms，等待传感器加载成功 */
	bflb_mtimer_delay_ms(40);

	/* 初始化AHT20 */
	AHT20_ChipInit();
	
	/* 等待AHT20芯片初始化完成 */
	bflb_mtimer_delay_ms(500);
	
	while (AHT20_STA_ERR == AHT20_ReadSta())    /* 直至AHT20为空闲状态且验证校准 */
	{
		/* 软复位AHT20 */
		AHT20_ChipSoftRes();          
		bflb_mtimer_delay_ms(200);
		
		AHT20_ChipInit();
		
		/* 等待AHT20芯片初始化完成 */
		bflb_mtimer_delay_ms(500);
		SoftResCount++;
		if (SoftResCount > 10)
		{
			/* 初始化10次失败 */
			return AHT20_INIT_RES_ERR;
		}
	}
	return AHT20_INIT_RES_OK;
}
AHT20ReadRes AHT20_RecTempAndHumiVal(int32_t *_THVal)
{
	uint8_t RecBuff[10] = {0};
	uint8_t WorkSta = 0;
	uint8_t ReadCount = 0;
	uint32_t RecDateTemp = 0;

	/* 启动芯片开始工作 */
	AHT20_ChipStartWork();

	/* 等到测试完成 */
	bflb_mtimer_delay_ms(80);

	/* 获取芯片状态 */
	AHT20_RecData(&WorkSta, 1);

	/* 检验芯片是否为空闲状态 */
	while ((WorkSta & 0x80) == 0x80)
	{
		bflb_mtimer_delay_ms(10);
		ReadCount++;
		if (ReadCount > 100)
		{
			/* 等待1S芯片依然处于忙的状态 */
			return AHT20_READ_ERR;
		}
		/* 重新读取状态 */
		AHT20_RecData(&WorkSta, 1);
	}

	/* 获取温湿度数据 */
	 AHT20_RecData(RecBuff, 7);

	 /* 湿度 */
	 RecDateTemp = 0;
	 RecDateTemp = (RecDateTemp | RecBuff[1]) << 8;
	 RecDateTemp = (RecDateTemp | RecBuff[2]) << 8; 
	 RecDateTemp = RecDateTemp | RecBuff[3];
	 RecDateTemp = RecDateTemp >> 4;
	 /* 数据转换，转换结果保留小数点后一位且放大10倍 */
	 *_THVal++ = RecDateTemp * 1000 /1024 /1024 ;
	 
	 /* 温度 */
	 RecDateTemp = 0;
	 RecDateTemp = (RecDateTemp | RecBuff[3]) << 8;
	 RecDateTemp = (RecDateTemp | RecBuff[4]) << 8; 
	 RecDateTemp = RecDateTemp | RecBuff[5];
	 RecDateTemp = RecDateTemp & 0xFFFFF;
	 /* 数据转换，转换结果保留小数点后一位且放大10倍 */
	 *_THVal = RecDateTemp * 200 * 10 / 1024 /1024 - 500 ;

	 return AHT20_READ_OK;
}